package workqueue;

import com.rabbitmq.client.*;
import utils.RabbitMQUtils;

import java.io.IOException;

public class Customer1 {
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        //每一次只能消费一个消息
        channel.basicQos(1);
        channel.queueDeclare("work",true,false,false,null);
        //参数1:队列名称  参数2:消息自动确认  true:消费者自动向rabbitmq确认消息消费   false:不会自动确认
        channel.basicConsume("work",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者-1:" + new String(body));
                //手动确认  参数1:手动确认消息标识,确认队列中具体哪个消息    参数2:false 每次确认一个，是否开启多个消息同时确认
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });


    }
}
